//下拉列表显示(游戏和购买方式)
function selectDo(gameId) {
	// 刷新区列表
	if(gameId != '' && gameId != null){
		$.ajax({
			type: "post",
			url: "/gameArea/select",
			data:{gameId:gameId},
			dataType:"json",
			async:false,
			success: function(data) {
				$("#area_id").siblings().remove();
				for (var i=0;i<data.length;i++) {
					 html = '<option value="' +data[i].val+ '">'+ data[i].txt +'</option>';
					 $("#area_id").after(html);
				}
			}
		});
	}else{
		$("#area_id").siblings().remove();
		$("#server_id").siblings().remove();
	}
	
	var val = $("#orderFlag").val();
	if (gameId == "") { // 判断选择请选择  删除选项
		if (val == 1) {
			$(".selectGL").siblings().remove();
			$(".selectGL").after('<option value="">补单</option>');
		} else {
			$(".selectGL").siblings().remove();
		}
		return false;
	}
	var html = "";
	if (val == 1) {
		$(".selectGL").siblings().remove();
		$(".selectGL").after('<option value="">补单</option>');
	} else {
		$.ajax({
			type: "post",
			url: "/buyConfig/select",
			data:{gameId:gameId},
			dataType:"json",
			async:false,
			success: function(data) {
				$(".selectGL").siblings().remove();
				for (var i=0;i<data.length;i++) {
					 html = '<option value="' +data[i].val+ '">'+ data[i].txt +'</option>';
					 $(".selectGL").after(html);
				}
			}
		});
	}
}
function selectType(val){
	if(val == 1){
		$(".selectGL").siblings().remove();
		$(".selectGL").after('<option value="">补单</option>');
	}else{
		selectDo($("#gameId").val());
	}
}
/*
之后要改 
 * @param value
 * @param row
 * @returns
 */
function buyFlagFormatter_point(value, row) {
	if (row.orderFlag == 1) {
		return "补单";
	}  else if (row.gameId == 1){
		return row.buyFlag;
	} else {
		if(row.buyTotalNum == 1){
			return "1单" +buyFlag;
		}else{
			return row.buyTotalNum + row.buyFlag
		}
	}
}
//购买商品
function goodsFormatter(value, row) {
	return row.buyTotalNum+row.buyNumUnit;
}
//已完成数量
function alreadyFinishNumFormatter(value, row) {
	if(row.alreadyFinishNum == null){
		return "0"+row.buyNumUnit;
	}else{
		return row.alreadyFinishNum+row.buyNumUnit;
	}
}
//实际获得
function actualGetNumFormatter(value, row) {
	return row.actualGetNum+row.buyNumUnit;
}
//订单金额
function orderTotalMoneyFormatter(value, row) {
	return row.orderTotalMoney+"元";
}

function payStatusFormatter(value, row) {
	if (row.payStatus == 1) {
        return ' 已支付 ';
    } else {
        return ' 未支付';
    }
}
function orderStatusFormatter(value, row) {
	if (row.orderStatus == -1) {
        return ' 已取消 ';
    } else if (row.orderStatus == 10) {
        return ' 未完成';
    } else if (row.orderStatus == 20) {
    	return ' 已完成';
    } else {
    	return ' 进行中';
    }
}
function desFormatter(value, row) {
	if (row.gameId == 1) {
		return "点赞";
	} else if (row.gameId == 3) {
		return "星贝";
	} else if (row.gameId == 4) {
		return "金币";
	} else if (row.gameId == 2) {
		return row.orderTypeValue;
	}
}
// 加载table 数据
$(function(){
    $("#dataTable").bootstrapTable({ 
    	url: "/order/list.json?random=" + Math.random(),
    	method:"get",
    	pagination:true,
    	sidePagination:"server",
    	cache:false,
    	queryParams: function(params) {
            return $.extend({}, params, parseJson(decodeURIComponent($("#dataForm").serialize())));     
        }
   }); 
 })
function buttonFormatter(value, row, index) {
	var caozuo4 = "<button id='btn_edit' type='button' onclick='checkPropertyValue(\""+row.ORDER_NO+"\");'  class='btn btn-default btn-sm'><span class='glyphicon glyphicon-pencil'></span>查看购买信息</button>";
	if (row.gameId == 1) {
		if (row.payStatus == 0 && row.orderStatus != -1) {//未支付
		var caozuo1 = "<button id='btn_edit' type='button' onclick='updatePayStatus("+row.rowId+", \""+row.orderNo+ "\");'  class='btn btn-default btn-sm'><span class='glyphicon glyphicon-pencil'></span>调单处理</button>";
		} else {
			var caozuo1 = "";
		}
		if (row.parentId == 0 && row.orderStatus != -1) {
			var caozuo2 = "<button id='btn_edit' type='button' onclick='toReplenish("+row.rowId+");'  class='btn btn-default btn-sm'><span class='glyphicon glyphicon-pencil'></span>补单</button>";
			var caozuo3 = "<button id='btn_edit' type='button' onclick='replenish_list("+row.rowId+");'  class='btn btn-default btn-sm'><span class='glyphicon glyphicon-eye-open'></span>补单记录</button>";
		} else {
			var caozuo2 = "";
			var caozuo3 = "";
		}
		return caozuo1+caozuo2+caozuo3+caozuo4;
	} else {
		return caozuo4+"";
	}
}

//调单处理
function updatePayStatus(rowId, orderNo){
   	layer.confirm('您确认支付<a style="color:red">'+'"'+orderNo+'"'+'</a>订单吗?', {
		btn: ['是', '否'] 
   	},
	function(){
   		if (rowId == null || rowId == "") {
    		layer.msg("id不能为空!");
    		return;
    	}
		var url = "/order/updatePayStatus?rowId="+rowId+"&orderNo="+orderNo;
    	$.post(url,null,function(data){
  		    if (data == 1) { 
  		   		layer.alert("支付此订单成功", {icon: 1});
  		   		findData();
  			} else {
  				layer.alert("支付此订单失败", {icon: 5});
  			}
		});
	},
	function(){ 
		return;
	});
}
//补单操作
function toReplenish(rowId){
	layer.open({
 	  	type: 2,
 	  	title: "补单操作",
 	  	closeBtn: 1,
 	  	shadeClose: true,
 	  	area: ['360px', '265px'],
 	  	content: '/order/toReplenish?rowId='+rowId,
 	  	btn: ["确 认","关 闭"], 
 	  	yes: function(index, layero) {
	    	var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
	        iframeWin.add();
		}
	});
}
//查看购买信息
function checkPropertyValue(orderNo){
	var base = new Base64();
	var encodeStr = base.encode(orderNo);
	layer.open({
 	  	type: 2,
	 	title: "查看链接信息",
 	  	closeBtn: 1,
 	  	shadeClose: true,
 	  	area: ['450px','230px'],
 	  	content: '/orderUrl/lookOver?orderNo='+encodeStr,
 	  	btn: ["关 闭"]
	});
}
//补单列表
function replenish_list(rowId){
	window.location.href="/order/replenishList?rowId="+rowId;
}
//查询
function findData(){
	var link = $("#PROPERTY_VALUE").val();
	var base = new Base64();
	var encodeStr = base.encode(link);
	if (encodeStr.indexOf("=") > 0) {
		var newEncodeStr = encodeStr.replace(/=/g, "等号");
		$('input[name="wm[t2.PROPERTY_VALUE]').val(newEncodeStr);
	} else {
		$('input[name="wm[t2.PROPERTY_VALUE]').val(encodeStr);
	}
	$("#dataTable").bootstrapTable("refresh",{} ); 
}
/*bootstrap table点击行事件*/
$(function(){
	 $("body").on("click","table>tbody>tr",function(){
      $(this).css("background","#e1ecf1").siblings().css("background","");
   });
})

//下拉列表显示游戏服
function listServer(areaId) {
		if(areaId != '' && areaId != null){
			$.ajax({
				type: "post",
				url: "/gameServer/select",
				data:{gameId:$("#gameId").val(),areaId:areaId},
				dataType:"json",
				async:false,
				success: function(data) {
					$("#server_id").siblings().remove();
					for (var i=0;i<data.length;i++) {
						 html = '<option value="' +data[i].val+ '">'+ data[i].txt +'</option>';
						 $("#server_id").after(html);
					}
				}
			});
		}else{
			$("#server_id").siblings().remove();
		}
}

// 录单页面
function addOrder(){
	layer.open({
	  	type: 2,
	  	title: "录单",
	  	closeBtn: 1,
	  	shadeClose: false,
	  	area: ['400px', '420px'],
	  	content: '/order/addOrder',
		btn: ["确 认","关 闭"], 
	  	yes: function(index, layero){
	    	var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
	        iframeWin.add();
	  	}
	});
}	